//
//  ZCScanInputVC.m
//  zctx
//
//  Created by apple on 16/4/12.
//  Copyright © 2016年 sanliang. All rights reserved.
//

#import "ZCScanInputVC.h"
#import "ZCScanDeatailsMoreVC.h"
#import "ZCRaiseVC.h"
@interface ZCScanInputVC ()
@property (weak, nonatomic) IBOutlet UIButton *backBtn;
@property (weak, nonatomic) IBOutlet UIButton *cancelBtn;
@property (weak, nonatomic) IBOutlet UITextField *textfiled;
@property (weak, nonatomic) IBOutlet UIView *searchView;
@end

@implementation ZCScanInputVC

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    self.view.shiftHeightAsDodgeViewForMLInputDodger = 50.0f;
    [self.view registerAsDodgeViewForMLInputDodger];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
    NSLog(@"显示了------");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.searchView.layer.cornerRadius = 35/2;
    [self.textfiled becomeFirstResponder];
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    //注册通知
    [center addObserver:self selector:@selector(textValueChanged:) name:UITextFieldTextDidChangeNotification object:self.textfiled];
    
}

#pragma mark - 取消操作
- (void)cancelAction{
    [self.textfiled resignFirstResponder];
    [self.navigationController popViewControllerAnimated:NO];
//    CATransition *animation = [CATransition animation];
//    animation.duration = 0.5;
//    animation.timingFunction = UIViewAnimationCurveEaseInOut;
//    animation.type = @"pageUnCurl";
//    animation.type = kCATransitionFade;
//    [self.view.window.layer addAnimation:animation forKey:nil];
//    
//    [self dismissViewControllerAnimated:YES completion:^{
//        
//    }];
}
- (IBAction)backBtnClick:(UIButton *)sender {
    [self cancelAction];
    
}
- (IBAction)cancelBtnClick:(UIButton *)sender {
    if (self.textfiled.text.length>0) {
        [self accordingQcode:self.textfiled.text];
    }else{
        [self cancelAction];
    }
}

#pragma mark - 扫描结果处理
- (void)accordingQcode:(NSString *)str
{
    
    [NetworkManager requestProductGetInfoByBarcodeWithMid:UserModel.mid barcode:str pagecount:@"0" success:^(NSDictionary *dictResponse) {
        
        
        NSObject *array = (NSObject *)[dictResponse objectForKey:@"content"];
        
        NSDictionary *more_dataDict = (NSDictionary *)[dictResponse objectForKey:@"more_data"];
        
        NSDictionary *more_faqDict = (NSDictionary *)[more_dataDict objectForKey:@"faq"];
        
        NSDictionary *more_buyDict = (NSDictionary *)[more_dataDict objectForKey:@"how_to_buy"];

        if ([array isKindOfClass:[NSDictionary class]]) {
            NSDictionary *dict =(NSDictionary *)array;
            if (dict.count>0) {
                if ([self.raiseStr isEqualToString:@"raise"]) {
                    NSString *strname = [dict objectForKey:@"name"];
                    NSDictionary *dict =[[NSDictionary alloc] initWithObjectsAndKeys:strname,@"title", nil];
                    NSNotification *notification =[NSNotification notificationWithName:@"scanInput" object:nil userInfo:dict];
                    [[NSNotificationCenter defaultCenter] postNotification:notification];
                    for (UIViewController *vc in self.navigationController.viewControllers) {
                        if ([vc isKindOfClass:[ZCRaiseVC class]]) {
                            [self.navigationController popToViewController:vc animated:YES];
                        }
                    }
                    NSLog(@"0000");
                }else{
                    NSLog(@"1111");
                    [self gotoScanDeatailsMore:dict andf:more_faqDict andb:more_buyDict];
                }
            }else{
                NSLog(@"22222");
                [self gotoScanDeatailsMore:nil andf:more_faqDict andb:more_buyDict];
            }
        }else{
            NSLog(@"33333暂无该条码信息");
            [[[iToast makeText:@"暂无该条码信息"]setGravity:iToastGravityCenter] show];
        }
        
    } failure:^(NSError *error) {
        NSLog(@"网络出错");
    }];
}

-(void)gotoScanDeatailsMore:(NSDictionary *)barDictionary andf:(NSDictionary *)fDictionary andb:(NSDictionary *)buyDictionary{
    
    NSLog(@"ssssss");
    
    ZCScanDeatailsMoreVC *vc = [[ZCScanDeatailsMoreVC alloc]init];
    vc.barcodeDictionary = barDictionary;
    vc.faqDictionary = fDictionary;
    vc.buyDictionary = buyDictionary;
    vc.barcodeStr = self.textfiled.text;
    vc.hidesBottomBarWhenPushed = YES;
    vc.intput = 1;
    [self.navigationController pushViewController:vc animated:NO];
}


//监听文本框的值的改变
- (void)textValueChanged:(NSNotification *)notice
{
    if (self.textfiled.text.length>0) {
        [self.cancelBtn setTitle:@"确认" forState:UIControlStateNormal];
    }else{
        [self.cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    }
}
//移除通知
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
